// 页面路径：store/index.js 
import Vue from 'vue'
import Vuex from 'vuex'
import {
	postlang
} from '@/api/all.js';

Vue.use(Vuex); //vue的插件机制

//Vuex.Store 构造器选项
const store = new Vuex.Store({
	state: { //存放状态
		userinfo: uni.getStorageSync('userinfo') ? JSON.parse(uni.getStorageSync('userinfo')) : '',
		usertoken: uni.getStorageSync('usertoken') ? uni.getStorageSync('usertoken') : '',
		shopinfo: uni.getStorageSync('shopinfo') ? JSON.parse(uni.getStorageSync('shopinfo')) : '',
		shoptoken: uni.getStorageSync('shoptoken') ? uni.getStorageSync('shoptoken') : '',
		employeeinfo: uni.getStorageSync('employeeinfo') ? JSON.parse(uni.getStorageSync('employeeinfo')) : '',
		employeetoken: uni.getStorageSync('employeetoken') ? uni.getStorageSync('employeetoken') : '',
		lang: uni.getStorageSync('lang') ? uni.getStorageSync('lang') : '',
		location: uni.getStorageSync('location') ? JSON.parse(uni.getStorageSync('location')) : '',
		address: uni.getStorageSync('address') ? JSON.parse(uni.getStorageSync('address')) : '',
		neworder: 0,
		tabelinfo: {
			shop_id: '',
			table_num: ''
		},
		shop: {
			totalprice: 0,
			totalnumber: 0,
			totalshop: ''
		},

		keyboard: false,
		//默认地址
		useraddress: [],
		//底部导航索引
		current: 0,
		neworder: 0,
		paid: 0
	},
	getters: {
		getuserinfo(state) {
			return state.userinfo
		},
		gettoken(state) {
			return state.usertoken
		},
		getlocation(state) {
			return state.location
		},
		getaddress(state) {
			return state.address
		},
		gettotalprice(state) {
			return state.shop.totalprice
		},
		gettotalshop(state) {
			return state.shop.totalshop
		},
		//获取默认地址
		getuseraddress(state) {

			return state.useraddress
		},
		getcurrent(state) {
			return state.current
		},
	},
	mutations: {
		//键盘监听--H5键盘有问题
		setKeyboard(state, keyboard) {
			state.keyboard = keyboard;
		},
		/******用户信息*********/
		setUserinfo(state, e) {
			uni.setStorageSync('userinfo', JSON.stringify(e))
			state.userinfo = e;
		},
		//token
		setUsertoken(state, e) {
			uni.setStorageSync('usertoken', e)
			state.usertoken = e;
		},
		/******商家信息*********/
		setShopinfo(state, e) {
			uni.setStorageSync('shopinfo', JSON.stringify(e))
			state.shopinfo = e;
		},
		//token
		setShoptoken(state, e) {
			uni.setStorageSync('shoptoken', e)
			state.shoptoken = e;
		},
		/******服务员信息*********/
		setEmployeeinfo(state, e) {
			uni.setStorageSync('employeeinfo', JSON.stringify(e))
			state.employeeinfo = e;
		},
		//token
		setEmployeetoken(state, e) {
			uni.setStorageSync('employeetoken', e)
			state.employeetoken = e;
		},
		//语言
		setLang(state, lang) {
			uni.setStorageSync('lang', lang)
			state.lang = lang;
		},
		//地址
		setLocation(state, location) {
			uni.setStorageSync('location', JSON.stringify(location))
			state.location = location;
		},
		//地址
		setAddress(state, address) {
			uni.setStorageSync('address', JSON.stringify(address));
			state.address = address;
		},
		setTotalprice(state, prams) {

			let newDatalist = state.shop.totalshop.categories[prams.index].products.map(_item => {
				if (_item.id == prams.item.id) {
					if (!_item.qty) {
						_item.qty = 1;
						state.shop.totalprice += prams.item.price;
						state.shop.totalnumber++;
					} else {
						if (prams.symbol == '+') {
							_item.qty += 1;
							state.shop.totalprice += prams.item.price;
							state.shop.totalnumber++;
						} else {
							_item.qty -= 1;
							state.shop.totalprice -= prams.item.price;
							state.shop.totalnumber--;
						}
					}
				}
				return _item;
			})
			state.shop.totalshop.categories[prams.index].products = newDatalist;

		},
		setTotalshop(state, item) {
			state.shop.totalshop = item;
		},
		clearShop(state) {
			let newdata = state.shop.totalshop.categories.map(item => {
				item.products.map(sub_item => {
					if (sub_item.qty) {
						sub_item.qty = 0;
					}
				})
				return item;
			})

			state.shop.totalshop.categories = newdata;
			state.shop.totalprice = 0;
			state.shop.totalnumber = 0;
		},
		//修改默认地址
		setUseraddress(state, useraddress) {
			state.useraddress = useraddress;
		},
		//修改底部导航
		setCurrent(state, current) {
			state.current = current;
		},
		Setshopinfo(state, shopinfo) {
			state.shop.totalshop = shopinfo;
		},
		Setneworder(state, order_num) {
			if (order_num) {
				state.neworder++;
			} else {
				state.neworder = 0;
			}
		},
		//桌子信息
		Settabelinfo(state, tabelinfo) {
			state.tabelinfo.shop_id = tabelinfo.shop_id;
			state.tabelinfo.table_num = tabelinfo.table_num;
		},

		//新订单生成
		Setneworder(state, order) {
			state.neworder = order;
		},
		//付钱成功
		Setpaid(state, paid) {
			state.paid = paid
		},


	}
})
export default store